package com.gitee.feizns.quickstart.mybatis.plus.jpa;

import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.annotation.FieldStrategy;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.extension.activerecord.Model;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.gitee.feizns.dynamic.bean.Convertible;
import com.gitee.feizns.quickstart.ddl.annotation.Comment;
import com.gitee.feizns.quickstart.mybatis.plus.plugins.inner.autofill.annotation.Now;
import com.vladmihalcea.hibernate.type.json.JsonStringType;
import lombok.Getter;
import lombok.Setter;
import org.hibernate.annotations.TypeDef;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.annotation.LastModifiedDate;

import javax.persistence.MappedSuperclass;
import java.io.Serializable;
import java.time.LocalDateTime;

/**
 * 通用字段父类
 * @author feizns
 * @since 2022/10/8
 */
@Getter
@Setter
@MappedSuperclass
@TypeDef(name = "json", typeClass = JsonStringType.class)
public abstract class Pojo<T extends Pojo<T, ID>, ID extends Serializable> extends Model<T> implements Convertible, Serializable {

    /**
     * 编号列名字
     */
    public static final String keyColumnName = "id";

    /**
     * 最后修改时间
     */
    @Now
    @Comment("最后一次更新时间")
    @LastModifiedDate
    @JsonProperty(index = Short.MAX_VALUE)
    @TableField(fill = FieldFill.INSERT_UPDATE, updateStrategy = FieldStrategy.ALWAYS)
    protected LocalDateTime lastModifiedTime;

    /**
     * 创建时间
     */
    @Now
    @Comment("创建时间")
    @JsonProperty(index = Integer.MAX_VALUE)
    @CreatedDate
    @TableField(fill = FieldFill.INSERT, updateStrategy = FieldStrategy.NEVER)
    protected LocalDateTime createTime;

}
